package com.example.recyclerdemo.room;

import android.app.Application;
import android.text.TextUtils;

import java.util.List;

public class StudentRepository {
    private final StudentRoomDao studentRoomDao;

    //初始化StudentRoomDao对象
    public StudentRepository(Application application) {
        StudentRoomDatabase db = StudentRoomDatabase.getInstance(application);
        studentRoomDao = db.getStudentRoomDao();
    }

    //在子线程中执行添加操作
    public void insert(final StudentEntity student) {
        StudentRoomDatabase.writeExecutor.execute(new Runnable(){
            @Override
            public void run() {
                studentRoomDao.insert(student);
            }
        });
    }

    //在子线程中执行更新操作
    public void update(final StudentEntity student){
        StudentRoomDatabase.writeExecutor.execute(new Runnable() {
            @Override
            public void run() {
                studentRoomDao.update(student);
            }
        });
    }

    //在子线程中执行删除操作
    public void delete(final StudentEntity student){
        StudentRoomDatabase.writeExecutor.execute(new Runnable() {
            @Override
            public void run() {
                studentRoomDao.delete(student);
            }
        });
    }

    //查询所有
    public List<StudentEntity> getAll(){
        return studentRoomDao.getAll();
    }

    //根据条件查询
    public List<StudentEntity>getAll(String kw){
        if (TextUtils.isEmpty(kw)){
            return studentRoomDao.getAll();
        }
        return studentRoomDao.getAll(kw);
    }


}
